﻿using System.ComponentModel.DataAnnotations;
using BootstrapAdmin.DataAccess.Models;

namespace BootstrapAdmin.Web.ViewModels.Production;

public sealed class SpotCheckSearchModel : ITableSearchModel
{
    [Display(Name = "点检项")]
    public string? Item { get; set; }

    [Display(Name = "点检结果")]
    public bool? IsPass { get; set; }

    [Display(Name = "班次")]
    public string? Shift { get; set; }

    [Display(Name = "记录时间")]
    public DateTimeRangeValue? CreatedTime { get; set; }

    public SpotCheckSearchModel()
    {
        Reset();
    }

    public IEnumerable<IFilterAction> GetSearchs()
    {
        var ret = new List<IFilterAction>();
        if (!string.IsNullOrEmpty(Item))
        {
            ret.Add(new SearchFilterAction(nameof(SpotCheck.Item), Item));
        }
        if (IsPass != null)
        {
            ret.Add(new SearchFilterAction(nameof(SpotCheck.IsPass), IsPass));
        }
        if (!string.IsNullOrEmpty(Shift))
        {
            ret.Add(new SearchFilterAction(nameof(SpotCheck.Shift), Shift));
        }

        if (CreatedTime != null)
        {
            ret.Add(new SearchFilterAction(nameof(SpotCheck.CreatedAt), CreatedTime.Start, FilterAction.GreaterThanOrEqual));
            ret.Add(new SearchFilterAction(nameof(SpotCheck.CreatedAt), CreatedTime.End, FilterAction.LessThanOrEqual));
        }

        return ret;
    }

    public void Reset()
    {
        Item = null;
        IsPass = null;
        Shift = null;
        CreatedTime = new DateTimeRangeValue
        {
            Start = DateTime.Now.AddDays(-7),
            End = DateTime.Now
        };
    }
}
